package com.tandashi.springbootstudy01.study.thread;

public class TestInterrupted extends Thread {
    @Override
    public void run() {
        super.run();
        try {
            for (int i = 0; i <500000; i++) {
                if(Thread.interrupted()){
                    System.out.println("已经停止状态了！我要退出了！");
                    throw new InterruptedException();
                }
                System.out.println("i = "+ (i+1));
            }
            System.out.println("我在for 下面");
        }catch (InterruptedException e){
            System.out.println("进入run 方法中的 catch 了 ");
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        try {
            TestInterrupted t1 = new TestInterrupted();
            t1.start();
            Thread.sleep(1000);
            t1.interrupt();

            System.out.println("是否停止11 "+t1.isInterrupted());//

            System.out.println("是否停止22 "+t1.isInterrupted());

            System.out.println("是否停止 1 = "+ Thread.currentThread().getName() + " "+Thread.interrupted());// main false
            System.out.println("是否停止 2 = "+ Thread.interrupted());// main false

            Thread.currentThread().interrupt();
            System.out.println("是否停止 1 ? = "+ Thread.currentThread().getName() +" "+Thread.interrupted()); //true
            System.out.println("是否停止 2 ？= "+ Thread.interrupted()); //false
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
